package com.eastchina.fids2focs.service;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import com.eastchina.fids2focs.common.AuthenticationManager;
import com.eastchina.fids2focs.common.FIDSIIError;
import com.eastchina.fids2focs.common.exception.FIDSIIValidateException;
import com.eastchina.fids2focs.domain.SystemUser;
import com.eastchina.fw.database.dao.QueryDAO;
import com.eastchina.fw.database.dao.UpdateDAO;


/**
 * LoginService 实现类
 * @author xiayg
 * 
 */
@WebService(serviceName="LoginService", 
		endpointInterface="com.eastchina.fids2focs.service.LoginService")
public class LoginServiceImpl implements LoginService {

	/**
	 * 查询DAO
	 */
	@Autowired
	QueryDAO queryDAO;
	
	/**
	 * 更新DAO
	 */
	@Autowired
	UpdateDAO updateDAO;
	
	@Autowired
	AuthenticationManager authMgr;
	
	/**
	 * 用户登陆处理
	 * @param username 用户名
	 * @param password 密码
	 * @return 用户情报
	 */
	public SystemUser login(String username, String password, String macAddr) throws FIDSIIValidateException {
		return authMgr.checkPermission(username, password, macAddr);
	}
	
	/**
	 * 用户注册
	 * @param username 用户名
	 * @param password 密码
	 * @return 用户情报
	 */
	public void register(SystemUser user) throws FIDSIIValidateException {
		
		// 获取用户情报
		SystemUser dbUser = queryDAO.executeForObject("SYSTEM_USER.select", user.getLoginId(), SystemUser.class);
		if (dbUser != null) {
			throw new FIDSIIValidateException("Validate error.", new FIDSIIError("sysmgr.AE1005"));	
		}
		
		// insert user
		updateDAO.execute("SYSTEM_USER.insert", user);
	}
}
